package utils.json;

import play.Logger;
import com.google.gson.JsonObject;
import models.Residence;
import models.User;

public class ResidenceJSON extends AbstractJSON {
	public ResidenceJSON(Residence residence) {
		put("id", residence.id);
		put("name", residence.name);
		User admin = User.getAdmins().get(0);
		if(admin != null) {
			put("admin", new UserResidenceJSON(admin));
		}
	}
	
	public ResidenceJSON(JsonObject object) {
		Long id = null;
		String name = object.get("name").getAsString();
		UserResidenceJSON admin = null;
		
		try {
			id = object.get("id") != null && !("").equals(object.get("id").getAsString()) ? object.get("id").getAsLong() : null;
			name = object.get("name").getAsString();
			if(object.get("admin") != null) {
				admin = new UserResidenceJSON(object.get("admin").getAsJsonObject());
			}
		} catch(Exception e) {
			Logger.error("Error parsing UserResidenceJSON from JSON object: %s", object.toString());
		}

		put("id", id);
		put("name", name);
		put("admin", admin);
		
		
	}
	
	public UserResidenceJSON getAdmin() {
		UserResidenceJSON admin = (UserResidenceJSON) get("admin");
		return admin;
	}
	
	public Residence getResidence() {
		Residence residence = new Residence();
		residence.id = (Long) get("id");
		residence.name = (String) get("name");
		
		return residence;
	}
}
